﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using SSOCommon;

namespace SSOServer
{
    /// <summary>
    /// VerifyService 的摘要说明
    /// </summary>
    public class VerifyService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (!VerifyParameters(context))
            {
                context.Response.Write("Arguments error.");
                context.Response.End();
            }
            Command cmd = Command.ParseCommand(context.Request.QueryString);
            Dispatcher(context, cmd);
        }

        private void Dispatcher(HttpContext context, Command cmd)
        {
            SSOAction action = (SSOAction)Enum.Parse(typeof(SSOAction), cmd.Action);
            switch (action)
            {
                case SSOAction.Login:
                    CoreServices.Login(context);
                    break;

                case SSOAction.Logout:
                    CoreServices.Logout(context);
                    break;

                case SSOAction.Auth:
                    CoreServices.Auth(context);
                    break;

                case SSOAction.GetUserInfo:
                    CoreServices.GetUserInfo(context);
                    break;
            }
        }

        /// <summary>
        /// Validate arguments which in QueryString
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private bool VerifyParameters(HttpContext context)
        {
            bool returnValue = true;
            if (Tools.GetParamValue(ArgumentConstant.SITEID) == String.Empty)
            {
                returnValue = false;
            }
            else if (Tools.GetParamValue(ArgumentConstant.ACTION) == String.Empty)
            {
                returnValue = false;
            }
            return returnValue;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}